let person = JSON.parse(localStorage.getItem('userinfo'));
// console.log(person);
let user_id = person.user_id;
let access_token = person.access_token;
// console.log(person);

// 获取
function getList(){
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/getList',
		type: 'get',
		dataType: 'json',
		data:{
			user_id,
			access_token,
		},
		success:function(res){
			// console.log(res);
			let html = '';
			let html1 = '';
			for(let i=0; i < res.data.length; i++){
				if(res.data[i].task_state == '0'){
					html +=`
							<div class="time">创建时间：${res.data[i].create_time}</div>
							<div class="shijian">
								<div class="shi">${res.data[i].task_name}</div>
								<div class="user_id">${res.data[i].user_id}</div>
								<div class="task_id">${res.data[i].task_id}</div>
								<div class="task_desc">${res.data[i].task_desc}</div>
								<div class="task_state">${res.data[i].task_state}</div>
								<img src="images/xiugai.png" alt="" class="xiugai">
								<img src="images/wancheng.png" alt="" class="wancheng">
							</div>
							`
				}else{
					html1 +=`
							<div class="time">创建时间：${res.data[i].create_time}</div>
							<div class="shijian">
								<div class="shi">${res.data[i].task_name}</div>
								<div class="user_id">${res.data[i].user_id}</div>
								<div class="task_id">${res.data[i].task_id}</div>
								<div class="task_desc">${res.data[i].task_desc}</div>
								<div class="task_state">${res.data[i].task_state}</div>
								<img src="images/xiugai.png" alt="" class="xiugai">
								<img src="images/shanchu.png" alt="" class="shanchu">
							</div>
							`
				}
			}
			$('.add').empty().append(html);
			$('.Completed').empty().append(html1);
		}
	})
}
getList();

// 添加
$('.jia .photo').click(function(){
	$('.add1').css('display','block');
})
$('.add1 .qu').click(function(){
	$('.add1').css('display','none');
})

$('.add1 .ti').click(function(){
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/add',
		type: 'post',
		dataType: 'json',
		data:{
			user_id,
			access_token,
			task_name:$('.item_name').val(),
			task_desc:$('.item_describe').val(),
		},
		success:function(res){
			// console.log(res);
			getList();
			$('.item_name').val("");
			$('.item_describe').val("");
			$('.add1').css('display','none');
		}
	})
})

// 修改
$('.add').on('click', '.xiugai', function(){
	task_name = $(this).parent().find('.shi').text();
	// console.log($(this).parent().find('.shi').text())
	// console.log(task_name);
	task_id = $(this).parent().find('.task_id').text();
	// console.log(task_id);
	task_desc = $(this).parent().find('.task_desc').text();
	// console.log(task_desc);
	task_state = $(this).parent().find('.task_state').text();
	// console.log(task_state);
	$('.modify').css('display','block');
	$('.modify .qu').click(function(){
		$('.modify').css('display','none');
	});
	
	$('.modify_task_name').val(task_name);
	// console.log($('.modify_task_name').val());
	$('.modify_task_desc').val(task_desc);
	$('.modify_task_state').val(task_state);
	
	$('.modify .ti').click(function(){
		let task_name = $('.modify_task_name').val();
		let task_desc = $('.modify_task_desc').val();
		let task_state = $('.modify_task_state').val();
		
		$.ajax({
			url:'http://chonghekj.com/todo/index.php/Home/Task/edit',
			type:'post',
			dataType: 'json',
			data:{
				user_id,
				access_token,
				task_id,
				task_name,
				task_desc,
				task_state,
			},
			success:function(res){
				alert('修改成功');
				$('.modify').css('display','none');
				getList();
			}
		})
	})
})
// 完成
$('.add').on('click', '.wancheng', function(){
	task_name = $(this).parent().find('.shi').text();
	task_id = $(this).parent().find('.task_id').text();
	task_desc = $(this).parent().find('.task_desc').text();
	task_state = '1';
	$.ajax({
		url:'http://chonghekj.com/todo/index.php/Home/Task/edit',
		type:'post',
		dataType: 'json',
		data:{
			user_id,
			access_token,
			task_id,
			task_name,
			task_desc,
			task_state,
		},
		success:function(res){
			alert('修改成功');
			// $('.modify').css('display','none');
			getList();
		}
	})
})

// 未完成
$('.Completed').on('click', '.xiugai', function(){
	task_name = $(this).parent().find('.shi').text();
	task_id = $(this).parent().find('.task_id').text();
	task_desc = $(this).parent().find('.task_desc').text();
	task_state = '0';
	$.ajax({
		url:'http://chonghekj.com/todo/index.php/Home/Task/edit',
		type:'post',
		dataType: 'json',
		data:{
			user_id,
			access_token,
			task_id,
			task_name,
			task_desc,
			task_state,
		},
		success:function(res){
			alert('修改成功');
			// $('.modify').css('display','none');
			getList();
		}
	})
})

// 删除
$('.Completed').on('click', '.shanchu', function(){
	task_id = $(this).parent().find('.task_id').text();
	// console.log(task_id);
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/delete',
		type: 'post',
		dataType: 'json',
		data:{
			user_id,
			access_token,
			task_id
		},
		success: function(res){
			console.log(res);
			getList();
		}
	})
})


// 登出
$('.sign_out .sign').click(function(){
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/User/logout',
		type: 'post',
		dataType: 'json',
		data:{
			user_id,
			access_token
		},
		success:function(res){
			// console.log(res);
			if(res.info == "登出成功"){
				location.href = `index.html`;
			}
		}
	})
})